package com.dt.app.controller.product;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dt.app.api.product.service.ICategoryService;
import com.dt.app.api.product.service.IProductMediaService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;
import com.dt.app.common.response.ResultUtil;
import com.dt.app.modules.product.entities.Product;
import com.dt.app.modules.product.vo.ProductBean;
import com.dt.app.api.product.service.IProductService;
import com.dt.app.controller.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author 鸿源塔基
 * @since 2024-03-07
 */
@RestController
@RequestMapping("api/v1/product/product")
public class ProductController extends BaseController{
    @Autowired
    private IProductService productService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IProductMediaService productMediaService;
    @ApiOperation(value = "列表")
    @RequestMapping("list")
    public ResultUtil<IPage<ProductBean>> list(ProductBean request){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(request.getPname())){
            wrapper.like("pname",request.getPname());
        }
        if(!StringUtils.isEmpty(request.getPno())){
            wrapper.eq("pno",request.getPno());
        }
        if(request.getCategoryId()!=null){
            wrapper.eq("category_id",request.getCategoryId());
        }
        wrapper.eq("company_id",this.getSysCompanyId());
        wrapper.orderByDesc("seq");
        Page page = new Page<>(request.getCurrent(),request.getSize());
        IPage listBody = productService.page(page,wrapper);
        List<Product> entityList=listBody.getRecords();
        if(!entityList.isEmpty()) {
            List<ProductBean> responseList = new ArrayList<>();
            entityList.forEach(it -> {
                ProductBean bean = new ProductBean();
                BeanUtils.copyProperties(it,bean);
                bean.setProductMedias(productMediaService.getProductMediaBeans(it.getProductId()));
                responseList.add(bean);
            });
            listBody.setRecords(responseList);
        }
        return ResultUtil.success(listBody);
    }

    @PostMapping("save")
    @ApiOperation(value = "新增")
    public ResultUtil save(@RequestBody @Valid Product entity){
        entity.setDeleted(0);
        entity.setCompanyId(this.getSysCompanyId());
        entity.setUtime(LocalDateTime.now());
        productService.save(entity);
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询")
    public ResultUtil<ProductBean> getById(@PathVariable String id){
        Product entity = productService.getById(id);
        if(entity!=null){
            ProductBean bean = new ProductBean();
            BeanUtils.copyProperties(entity,bean);
            if(entity.getCategoryId()!=null)
                bean.setCname(categoryService.getById(entity.getCategoryId()).getCname());
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改")
    public ResultUtil update(@RequestBody @Valid Product entity){
        productService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        productService.removeById(id);
        return ResultUtil.success();
    }
}